﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using YiSha.Business.YiDongYuan;
using YiSha.Entity.YiDongYuan;
using YiSha.Model.Param;
using YiSha.Model.Param.OrganizationManage;
using YiSha.Model.Param.YiDongYuan;
using YiSha.Util.Extension;
using YiSha.Util.Model;

namespace YiSha.Admin.WebApi.Controllers
{
    /// <summary>
    /// 机械信息控制器
    /// </summary>
    [Route("[controller]/[action]")]
    [ApiController]
    [AuthorizeFilter]
    public class MechanicsController : ControllerBase
    {
        private MechanicsBLL mechanicsBLL = new MechanicsBLL();
        private MechanicsApplyBLL mechanicsApplyBLL = new MechanicsApplyBLL();

        #region 获取数据

        /// <summary>
        /// 获取机械列表
        /// </summary>
        /// <param name="param"></param>
        /// <param name="pagination"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<TData<List<MechanicsEntity>>> GetPageList([FromQuery] MechanicsApplyParam param, [FromQuery] Pagination pagination)
        {
            TData<List<MechanicsEntity>> obj = await mechanicsBLL.GetPageList(param, pagination);
            return obj;
        }

        /// <summary>
        /// 获取机械申报列表
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<TData<List<MechanicsApplyEntity>>> GetListByUser([FromQuery] long userId)
        {
            TData<List<MechanicsApplyEntity>> obj = await mechanicsApplyBLL.GetListByUserId(userId);
            return obj;
        }

        /// <summary>
        /// 根据关键字查询机械列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<TData<List<MechanicsEntity>>> Search([FromQuery] MechanicsApplyParam param)
        {
            TData<List<MechanicsEntity>> obj = await mechanicsBLL.GetList(param);
            return obj;
        }

        /// <summary>
        /// 根据id获取机械内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<TData<MechanicsEntity>> GetForm([FromQuery] long id)
        {
            TData<MechanicsEntity> obj = await mechanicsBLL.GetEntity(id);
            return obj;
        }

        /// <summary>
        /// 根据id获取机械备案申请内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<TData<MechanicsApplyEntity>> GetApplyFormById([FromQuery] long id)
        {
            TData<MechanicsApplyEntity> obj = await mechanicsApplyBLL.GetEntity(id);
            return obj;
        }

        /// <summary>
        /// 根据环保号牌获取机械内容
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<TData<MechanicsEntity>> GetFormByCode([FromQuery] string code)
        {
            TData<MechanicsEntity> obj = await mechanicsBLL.GetEntity(code);
            return obj;
        }

        #endregion 获取数据

        #region 提交数据

        /// <summary>
        /// 保存机械数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<TData<string>> SaveForm([FromBody] MechanicsEntity entity)
        {
            TData<string> obj = await mechanicsBLL.SaveForm(entity);
            return obj;
        }

        /// <summary>
        /// 保存申请备案
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<TData<string>> SaveApply([FromForm] MechanicsApplyEntity entity)
        {
            if (entity.OwnerName.IsNullOrEmpty())
                throw new Exception("请输入所有人姓名");
            if (entity.OwnerTel.IsNullOrEmpty())
                throw new Exception("请输入联系电话");
            if (entity.OwnerCardNo.IsNullOrEmpty())
                throw new Exception("请输入证件号码");
            if (entity.OwnerCard_pic.IsNullOrEmpty())
                throw new Exception("请上传证件照片");
            if (entity.JX1_pic.IsNullOrEmpty())
                throw new Exception("请上传机械照片");
            if (entity.JXDJLX.IsNullOrEmpty())
                throw new Exception("请选择机械登记类型");
            if (entity.PFJD.IsNullOrEmpty())
                throw new Exception("请选择排放阶段");
            if (entity.Region.IsNullOrEmpty())
                throw new Exception("请选择所属地区");
            entity.AuditTime = null;
            entity.AuditUserId = null;
            entity.AuditStatus = 0;
            return await mechanicsApplyBLL.SaveForm(entity);
        }

        #endregion 提交数据
    }
}